package com.jd.sunny.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;

/**
 * 图片工具类
 *
 * @author :孙林
 * @ClassName:ImageUtil
 * @Description:
 * @date :2012-9-10上午10:07:10
 */


public abstract class ImageUtil {

    private static final int IMAGE_WIDTH = 200;
    private static final int IMAGE_HEIGHT = 50;
    private static final int IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;
    private static final String IMAGE_FORMAT = "JPG";

    public static void getImage() throws IOException {
        BufferedImage bufferedImage = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_TYPE);
        Graphics graphics = bufferedImage.getGraphics();
        graphics.setColor(Color.LIGHT_GRAY);
        graphics.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);

        graphics.setColor(Color.BLACK);
        graphics.setFont(new Font("", Font.PLAIN, 20));
        Random random = new Random();
        int number = random.nextInt(9999);
        graphics.drawString(String.valueOf(number), 75, 35);

        for (int i = 0; i < 500; i++) {
            int x = random.nextInt(IMAGE_WIDTH);
            int y = random.nextInt(IMAGE_HEIGHT);
            graphics.drawOval(x, y, 2, 1);
        }

        ImageIO.write(bufferedImage, IMAGE_FORMAT, new File("C:/1.jpg"));

    }

    public static void main(String[] args) throws IOException {
        getImage();
    }
}
